<?php
	class ModelNewsNewsCategory extends Model
	{
		public function addNews($data) 
		{
			$this->db->query("INSERT INTO " . DB_PREFIX . "news_category SET sort_order = '" . (int)$data['sort_order'] . "',created_date = NOW(), updated_date = NOW(),  status = '" . (int)$data['status'] . "'" );
	
			$news_category_id = $this->db->getLastId();
			
			foreach ($data['news_category_description'] as $language_id => $value) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "news_category_description SET news_category_id = '" . (int)$news_category_id . "',language_id = '" . (int)$language_id . "', news_name = '" . $this->db->escape($value['news_name']) . "', description = '" . $this->db->escape($value['description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "'");
			}
						
			if ($data['keyword']) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_category_id=" . (int)$news_category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
			}
			
			$this->cache->delete('news_category_id');

		}
		
		public function editNews($news_category_id, $data) 
		{
		
			$this->db->query("UPDATE " . DB_PREFIX . "news_category SET sort_order = '" . (int)$data['sort_order'] . "', created_date = NOW(), updated_date = NOW(),  status = '" . (int)$data['status'] . "' WHERE news_category_id = '" . (int)$news_category_id . "'");

			$this->db->query("DELETE FROM " . DB_PREFIX . "news_category_description WHERE news_category_id = '" . (int)$news_category_id . "'");

			foreach ($data['news_category_description'] as $language_id => $value) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "news_category_description SET news_category_id = '" . (int)$news_category_id . "', language_id = '" . (int)$language_id . "', news_name = '" . $this->db->escape($value['news_name']) . "', description = '" . $this->db->escape($value['description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "'");
			}
							
			$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_category_id=" . (int)$news_category_id. "'");
			
			if ($data['keyword']) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_category_id=" . (int)$news_category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
			}
			
			$this->cache->delete('news_category');
		}
		
		public function deleteNews($news_category_id) 
		{
			$this->db->query("DELETE FROM " . DB_PREFIX . "news_category WHERE news_category_id = '" . (int)$news_category_id . "'");
			$this->db->query("DELETE FROM " . DB_PREFIX . "news_category_description WHERE news_category_id = '" . (int)$news_category_id . "'");
			//$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");
			$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_category_id=" . (int)$news_category_id . "'");
		
			$this->cache->delete('news_category');
		}
		
		public function getNew($news_category_id) //lay ra tu khoa khi search
		{
			$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'news_category_id=" . (int)$news_category_id . "') AS keyword FROM " . DB_PREFIX . "news_category WHERE news_category_id = '" . (int)$news_category_id . "'");

			return $query->row;
		} 
		
		public function getPath($news_category_id) {
		$query = $this->db->query("SELECT news_name FROM " . DB_PREFIX . "news_category c LEFT JOIN " . DB_PREFIX . "news_category_description cd ON (c.news_category_id = cd.news_category_id) WHERE c.news_category_id = '" . $news_category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.news_name ASC");
		
		return $query->row['news_name'];
	}
		
		public function getNewsCategories($parent_id = 0) {
			$category_data = array();
		
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_category c LEFT JOIN " . DB_PREFIX . "news_category_description cd ON (c.news_category_id = cd.news_category_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.news_name ASC");
		
			foreach ($query->rows as $result) {
				$category_data[] = array(
					'news_category_id' => $result['news_category_id'],
					'news_name'        => $this->getPath($result['news_category_id'], $this->config->get('config_language_id')),
					'status'  	  => $result['status'],
					'sort_order'  => $result['sort_order']
				);
				
				//$category_data = array_merge($category_data, $this->getCategories($result['category_id']));
			}	
		
			return $category_data;
	}
	
	public function getNew1($news_category_id) 
	{
		$news_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_category_description WHERE news_category_id = '" . (int)$news_category_id . "'");
		
		foreach ($query->rows as $result) {
			$news_data[$result['language_id']] = array(
				'news_name'        => $result['news_name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}
		
		return $news_data;
	}	
		
		public function getNews($news_id) 
		{
			$news_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$new_id . "'");
			
			foreach ($query->rows as $result) {
				$news_data[$result['language_id']] = array(
					'news_name'        => $result['news_name'],
					'meta_keyword'     => $result['meta_keyword'],
					'meta_description' => $result['meta_description'],
					'description'      => $result['description']
				);
			}
			
			return $news_data;
		}	
		
		public function getNewsStores($news_id) 
		{
			$news_store_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_store WHERE news_id = '" . (int)$news_id . "'");

			foreach ($query->rows as $result) {
				$news_store_data[] = $result['store_id'];
			}
			
			return $news_store_data;
		}
		
		public function getNewsLayouts($news_id) 
		{
			$news_layout_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_layout WHERE news_id = '" . $this->db->escape($value['news_id']) . "'");
			
			foreach ($query->rows as $result) {
				$news_layout_data[$result['store_id']] = $result['layout_id'];
			}
			
			return $news_layout_data;
		}
	}
?>